<?php

use app\api\extend\DataCache;
use think\Db;

/**
 *  获取后台管理员的菜单列表
 */
function get_admin_navs($token)
{
    $result    = Db::name('tokens')->where(['token' => $token])->find();
    $user_id   = isset($result['user_id']) ? $result['user_id'] : 0;
    $user_type = isset($result['user_type']) ? $result['user_type'] : 'user_index';

    switch ($user_type) {
        case 'user_index':
            $role_ids = Db::name('users')->where(['id' => $user_id, 'data_state' => 1])->value('role_ids');
            break;
        case 'admin':
            $role_ids = Db::name('admins')->where(['id' => $user_id, 'data_state' => 1])->value('role_ids');
            break;
        default:
            $role_ids = '';
            break;
    }

    $nav = [];
    if ($role_ids) {
        // 菜单权限
        $nav_powers = Db::name('role_nav_powers')->where(['role_id' => ['in', $role_ids], 'data_state' => 1])->select();
        foreach ($nav_powers as $value) {
            if ($value['nav_ids']) {
                $arr = explode(',', $value['nav_ids']);
                foreach ($arr as $value1) {
                    if (!in_array($value1, $nav)) {
                        array_push($nav, $value1);
                    }
                }
            }
        }
    }

    $data = Db::name('navs')->where(['data_state' => 1, 'id' => ['in', $nav], 'status' => 1])->order('pid desc,sort desc')->select();
    foreach ($data as $key => $value) {
        if ($value['module'] && $value['class'] && $value['method']) {
            $data[$key]['href'] = get_host() . '/' . $value['module'] . '/' . $value['class'] . '/' . $value['method'];
        } else {
            $data[$key]['href'] = '';
        }
    }
    $left_nav = list_to_tree($data);

    return $left_nav;
}

/**
 *  根据经纬度计算两地距离
 */
function get_distance($lng1,$lat1,$lng2, $lat2)
{
    // 地球半径
    $earthRadius = 6367000;
    $lat1 = ($lat1 * pi()) / 180;
    $lng1 = ($lng1 * pi()) / 180;
    $lat2 = ($lat2 * pi()) / 180;
    $lng2 = ($lng2 * pi()) / 180;

    $calcLongitude = $lng2 - $lng1;
    $calcLatitude  = $lat2 - $lat1;
    $stepOne       = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
    $stepTwo       = 2 * asin(min(1, sqrt($stepOne)));
    $distance      = $earthRadius * $stepTwo;

    if ($distance < 1000) {
        $result = round($distance);
    } else {
        $result = round(($distance / 1000)) . 'km';
    }

    return $result;
}

/**
 *  查找IDs集
 *  params：key字段
 *  params：value值
 *  params：table表名
 *  params：is_exact是否精确匹配(默认启用模糊查询)
 */
function get_ids($key, $value, $table, $is_exact = 0)
{
    $map = [];
    if ($is_exact == 0) {
        $map = [
            $key         => ['like', '%' . $value . '%'],
            'data_state' => 1,
        ];
    } elseif ($is_exact == 1) {
        $map = [
            $key         => $value,
            'data_state' => 1,
        ];
    }

    $ids = Db::name($table)->where($map)->column('id');
    return $ids;
}

/**
 *  校验layui的认证与授权
 *  params：auth认证码
 *  params：token授权码
 */
function layui_check_power($auth, $token)
{
    $system_auth = DataCache::getAuth();
    if ($auth !== $system_auth) {
        return 0;
    }

    $check_token = Db::name('tokens')->where(['token' => $token, 'is_mobile' => 0])->find();
    if ($check_token) {
        $time_length = time() - $check_token['create_time'];
        // 时间长度大于1天，重新登录
        if ($time_length >= 86400) {
            return 0;
            // 时间长度大于过期时间（2小时）且小于1天，刷新token
        } elseif ($time_length < 86400 && $time_length > $check_token['expire_time']) {
            return 0;
        }
    } else {
        return 0;
    }

    return 1;
}

/**
 *  创建产品的唯一序列号
 */
function make_uuid()
{
    $chars = md5(uniqid(mt_rand(), true));
    $uuid  = substr($chars, 0, 8) . '-';
    $uuid .= substr($chars, 8, 4) . '-';
    $uuid .= substr($chars, 12, 4) . '-';
    $uuid .= substr($chars, 16, 4) . '-';
    $uuid .= substr($chars, 20, 12);
    return $uuid;
}

/**
 *  更新第三方配置文件
 */
function update_config_file($data)
{
    $config_file = 'application/api/config-' . WANCLL_ENV . '.php';
    if (file_exists($config_file)) {
        $configs = include $config_file;
    } else {
        $configs = array();
    }

    $configs = array_merge($configs, $data);
    $result  = file_put_contents($config_file, "<?php\nreturn " . var_export($configs, true) . ";");

    return $result;
}
